/**************************************************************************************************/
/**
 * @file      : main.c
 * @brief     : CAN example file.
 * @version   : V1.8.0
 * @date      : May-2020
 * @author    : Zhixin Semiconductor
 *
 * @note      : This example contains sample code for customer evaluation purpose only. It is not
 * part of the production code deliverables. The example code is only tested under defined
 * environment with related context of the whole example project. Therefore, it is not guaranteed
 * that the example always works under user environment due to the diversity of hardware and
 * software environment. Do not use pieces copy of the example code without prior and separate
 * verification and validation in user environment.
 *
 * @copyright : Copyright (c) 2020-2023 Zhixin Semiconductor Ltd. All rights reserved.
 **************************************************************************************************/


#include "SysClk.h"
#include "GPIO.h"	
#include "ADC.h"	
#include "CAN.h"
#include "drv_task.h"
#include "pltask.h"
#include "softtimer.h"
#include "MainTask.h"
#include "drv_uart.h"
#include "Z20K11xM_uart.h"
#include "Z20K118M.h"

static SOFTTIMER_TIMEOUT startup_timeout;


void System_Init(void)
{
	Ex_Clock_Init();
	Ex_Gpio_Init();
	Ex_Can_Init();
	Ex_Adc_Init();
	//Ex_Task_Init();
	Ex_Uart_Init();
}

void Platform_Init(void){

}
unsigned char uart_tx_data = 0x55;


static SOFTTIMER_TIMEOUT sys_timeout;
static SOFTTIMER_TIMEOUT startup_timeout;
static SOFTTIMER_TIMEOUT turn_timeout = {0ul, 0ul};

unsigned int key = 0x00U;
int main(void)
{
  /* Disable wdog */
  SYSCTRL_EnableModule(SYSCTRL_WDOG);
  WDOG_Disable();
	
	System_Init();
    plrti_init();
	SysTick_Init();
   

    
    
    SOFTTIMER_StartTimeOut(&sys_timeout, SW_TMR_MS(APP_SHUTDOWN_TIMEOUT));
    SOFTTIMER_StartTimeOut(&startup_timeout, SW_TMR_MS(APP_STARTUP_TIMEOUT));
    
 
    SOFTTIMER_Start();
    
    GPIO_TogglePinOutput(PORT_E, GPIO_7);
    //uart_reg_w_t * UARTxw = (uart_reg_w_t *)(PERIPHERAL_BASE_ADDR + 0x6B000UL);
    
    Ex_Uart_Dma_Init();
    
    GPIO_SetPinOutput(PORT_E, GPIO_7);
    SOFTTIMER_ResetTimeOut(&sys_timeout);
          
	for(;;)
	{
        
//        if (SOFTTIMER_IsTimeoutReached(&startup_timeout)) {
//            key = GPIO_ReadPinLevel(PORT_D, GPIO_1);
//            if ((key == 0) /* && SOFTTIMER_IsTimeoutReached(&turn_timeout) */) {
//                if (SOFTTIMER_IsTimeoutReached(&sys_timeout)) {
//                    GPIO_ClearPinOutput(PORT_E, GPIO_7);
//                }
//            } else {
//                 GPIO_SetPinOutput(PORT_E, GPIO_7);
//                SOFTTIMER_ResetTimeOut(&sys_timeout);
//            }  
//        }
        
        if (SOFTTIMER_IsTimeoutReached(&startup_timeout)) {
            
            key = GPIO_ReadPinLevel(PORT_D, GPIO_1);
            if (key == 0){
                
                DMA_ChannelRequestEnable(DMA_CHANNEL0);
                /* DMA should be halted when refereshing or unlock wdog, and after that continuing DMA transfer*/
//                DMA_HaltControl(ENABLE);
//                WDOG_Refresh();
//                DMA_HaltControl(DISABLE);

                while (DMA_GetDoneStatus(DMA_CHANNEL0) != SET);
            }
            GPIO_TogglePinOutput(PORT_E, GPIO_7);
//            for(unsigned int i = 0x00U; i < 0x400;i++){
//              UART_SendByte( UART1_ID, uart_tx_data);
//            }
           SOFTTIMER_ResetTimeOut(&startup_timeout);
        }
//      plrti_idle_for_timer_task();
//		WDOG_Refresh();
	
}


            /* Send data */
//            UARTxw->UART_RBR_THR_DLL.UART_THR = (uint32_t)0x55;
//            UARTxw->UART_RBR_THR_DLL.UART_THR = (uint32_t)0xAA;
//            UARTxw->UART_RBR_THR_DLL.UART_THR = (uint32_t)0x55;
//            UARTxw->UART_RBR_THR_DLL.UART_THR = (uint32_t)0xAA;
                
}              